import moment from 'moment';
import _ from 'lodash';
import { NumberFormat2 } from '@/utils';
const FORMATDAY = 'YYYY-MM-DD';
const MONMENT_SPIT = {
  week: 'isoWeek',
  month: 'month',
  year: 'year',
};

export const getDisabledDate = (type = 'week') => {
  return moment()
    .add('days', -1)
    .endOf(MONMENT_SPIT[type] || 'month');
};
export const getSanKeyOption = function (nodes, links) {
  let series = []; //图表显示数据的集合
  let extendArr = [];
  let tooltip = {
    trigger: 'item',
    triggerOn: 'mousemove',
    extraCssText: 'box-shadow: 0 0 10px rgba(0, 0, 0, 0.1)',
    backgroundColor: '#161A1D',
    textStyle: { color: '#888E95', borderWidth: 1, borderColor: '#1A253C4B', padding: 15 },
  };
  tooltip.axisPointer = {
    type: 'cross',
    label: { backgroundColor: '#F5F8FA', color: '#888E95', shadowBlur: 0 },
  };
  tooltip.formatter = (params) => {
    let arr = params.name.split('>');
    let name =
      arr.length > 1
        ? arr[0].split('(')[0].trim() + '-' + arr[1].split('(')[0].trim()
        : arr[0].split('(')[0].trim();
    return `<div>${name}：<span style="color: #fff;">${params.value}万kWh</span></div>`;
  };
  let serie = { type: 'sankey', layout: 'none', focusNodeAdjacency: 'allEdges' };
  serie.left = 0;
  serie.right = '19%';
  serie.draggable = false;
  let nodeArray = _.uniqBy(nodes, 'name').map((item) => {
    if (item.typeEditing) {
      extendArr.push(item.name);
    }
    let tvalue = Number(item.quantity) || 0;
    return {
      label: {
        color: '#fff',
        show: true,
        formatter: (n) => {
          return n.name + item.detail;
        },
      },
      depth: item.depth || 0,
      name: item.name,
      xvalue: tvalue,
      value: NumberFormat2(Math.abs(tvalue), 3),
      itemStyle: {
        color: tvalue < 0 ? '#ED5945' : item.color,
        borderWidth: item.borderColor ? 1 : 1,
        borderColor: item.borderColor || item.color,
      },
    };
  });
  serie.data = nodeArray;
  serie.links = links.map((item) => {
    return {
      source: item.source,
      target: item.target,
      value: NumberFormat2(Math.abs(item.quantity), 3),
      xvalue: Number(item.quantity),
    };
  });
  serie.lineStyle = { color: 'source', curveness: 0.3 };
  serie.nodeGap = 20;
  serie.layoutIterations = 0;
  serie.focusNodeAdjacency = false;
  series.push(serie);
  return {
    grid: { left: 0, top: 20, right: '30%', height: '100%', containLabel: true },
    toolbox: { show: false },
    tooltip: tooltip,
    series: series,
  };
};
export const getStartAndEndDate = (date = moment(), type = 'week') => {
  let startDate = moment(date),
    endDate = moment(date);
  const configMoment = MONMENT_SPIT[type];
  if (configMoment) {
    startDate = startDate.startOf(configMoment);
    endDate = endDate.endOf(configMoment);
  }
  startDate = startDate.format(FORMATDAY);
  endDate = endDate.format(FORMATDAY);
  return { startDate, endDate };
};

export const powerType = [
  {
    code: '全部',
    name: '全部',
  },
];
